/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ocumed.teams.teamd.application;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import ocumed.teams.IMediver;

/**
 *
 * @author Willi
 */
public class VerordnungTableModel extends DefaultTableModel {

    private List<IMediver> m_data;
    private List<IMediver> m_modeldata;

    public VerordnungTableModel(Set<IMediver> med) {
        super();

        try {

            // �berschriften der JTable
            Vector<String> vec = new Vector<String>();

            vec.add("Medikament");
            vec.add("Packungsgroesse");
            vec.add("Dosierung");
            vec.add("Menge");
            vec.add("Von");
            vec.add("Bis");

            setColumnCount(vec.size() - 1);
            setColumnIdentifiers(vec);

            m_data = setToList(med);
            m_modeldata = m_data;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public List<IMediver> setToList(Set<IMediver> med) {
         List<IMediver> beh = new ArrayList<IMediver>();
        
        for (IMediver b: med){
            beh.add(b);
        }
        
        return beh;
    }

    public int getId(int row) {
        IMediver m = m_modeldata.get(row);

        return m.getMediverid();
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        if (column == 2 || column == 3 || column == 5) {
            return true;
        }
        return false;
    }

    @Override
    public Object getValueAt(int row, int col) {
        if (m_modeldata != null) {
            IMediver p = m_modeldata.get(row);
            if (p != null) {
                switch (col) {
                    case 0:
                        return p.getiMedikament().getMedikamentname();
                    case 1:
                        return p.getiMedikament().getMedikamentpackungsgroesse();
                    case 2:
                        return p.getMediverdosierung();
                    case 3:
                        return p.getMediVerMenge();
                    case 4:
                        return DateTimeToString(p.getMediVerVon());
                    case 5:
                        return DateTimeToString(p.getMediVerBis());
     
                }
            }
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int row, int col) {
        if (m_modeldata != null) {
            IMediver p = m_modeldata.get(row);
            if (p != null) {
                switch (col) {
                    case 0:
                        
                        break;
                    case 1:
                        
                        break;
                    case 2:
                        p.setMediverdosierung((String) aValue);
                        break;
                    case 3:
                        p.setMediVerMenge(Integer.parseInt((String) aValue));
                        break;

                    case 5:
                        try {
                            p.setMediVerBis(new SimpleDateFormat("dd.MM.yyyy").parse((String) aValue));
                        } catch (ParseException ex) {
                            
                        }
                        break;

                }
            }
        }
    }
    
    private static String DateTimeToString(Date datum) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "dd.MM.yyyy");
        cal.setTime(datum);

        return sdf.format(cal.getTime());
    }

    @Override
    public int getRowCount() {
        if (m_modeldata != null) {
            return m_modeldata.size();
        } else {
            return 0;
        }
    }
}
